package user

import (
	"errors"
	"github.com/gin-gonic/gin"
	"github.com/jinzhu/gorm"
	"server/base"
	pool2 "server/common/models/pool"
	user3 "server/common/models/user"
	"server/common/utils"
	"server/pkg/response"
	"time"
)

var Pool = new(pool)

type pool struct{}

func (*pool) Register(c *gin.Context) {
	userId := utils.GetUserId(c)
	var users user3.User
	base.Mysql.Find(&users, "user_id = ?", userId)
	if users.Gender == 0 {
		response.Error(c, "请先完善个人信息")
		return
	}

	var PoolRegister pool2.PoolUserRegister
	err := base.App.Mysql.Object.Transaction(func(tx *gorm.DB) error {
		tx.Find(&PoolRegister, "user_id = ? and ( status = 'wait' ||  status = 'doing' )", userId)
		if PoolRegister.UserId != 0 {
			return errors.New("您已经报名过了哦。")
		}
		PoolRegister.UserId = userId
		PoolRegister.Status = "wait"
		PoolRegister.UserGender = users.Gender
		PoolRegister.CreateTime = time.Now().Unix()
		PoolRegister.UpdateTime = time.Now().Unix()
		tx.Create(&PoolRegister)
		return nil
	})
	if err != nil {
		response.Error(c, err.Error())
		return
	}
	response.Success(c, gin.H{})
}
